package com.hd.trans.network.component

import android.text.TextUtils
import android.util.Log
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.hd.trans.TranslationInterior
import com.hd.trans.network.ApiServiceManager
import com.hd.trans.network.RxSchedulers
import com.hd.trans.network.bean.UserInfoBean
import com.hd.trans.network.bean.aiChat.AiChatResponse
import com.hd.trans.network.bean.aiChat.AiChatTaskRequest
import com.hd.trans.ui.service.NetDisposableObserver3

class AiChatManager: LifecycleObserver {
    private var isDestroy = false
    private var callback:((AiChatResponse)->Unit)? = null
    private var mTranslateComponent: HdTranslateComponent = HdTranslateComponent() //文本翻译组件
    private var systemSetting: String? = null
    private var history: MutableList<String>? = null
    private var userinfoBean: UserInfoBean? = null
    private var model: String? = null
    private var strict: Int = 0
    private var limit: Int = 0
    /**
     * 页面创建 初始化回调
     */

    /**
     * 页面销毁 终止回调
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun destroy() {
        isDestroy = true
        mTranslateComponent.destroy()
    }

    /**
     * @param input 输入内容
     * * @param userinfoBean 用户信息
     * @param systemSetting 系统设置
     * @param history 历史记录
     * @param model AI模型, 默认:32k, 可选: 32k,128k,256k,deepseek,deepseekv3, 推荐使用deepseek, deepseekv3速度快没有深度思考
     * @param strict 文本检测严格模式,用于上架审核等情况，0：非严格模式，1：严格模式
     * @param limit 是否开启对用户的使用次数受后台预设限制. 0:关闭,1:开启, 默认:0
     * @param back 回调
     */
    fun chatSSE(
        input: String,
        userinfoBean: UserInfoBean,
        systemSetting: String? = null,
        history: MutableList<String>? = null,
        model: String? = null,
        strict: Int = 0,
        limit: Int = 0,
        back: ((AiChatResponse) -> Unit)? = null
    ) {
        callback = back
        this.systemSetting = systemSetting
        this.history = history
        this.userinfoBean = userinfoBean
        this.model = model
        this.strict = strict
        this.limit = limit

        makeChatTask(input)
    }


    private fun makeChatTask(input: String) {
        if(TextUtils.isEmpty(input)){
            callback?.invoke(AiChatResponse(-1, "", "内容不能为空", ""))
            return
        }
        /*ServerSentEvents.chatSSE(
            AiChatTaskRequest(
                input,
                systemSetting,
                history,
                strict,
                model ?: "deepseek"
            ).makeRequestBody(userinfoBean)
        )*/
        ApiServiceManager.aiTransService.chatSSE(
            AiChatTaskRequest(
                input,
                systemSetting,
                history,
                strict,
                limit,
                model ?: "deepseekv3"
            ).makeRequestBody(userinfoBean)
        )
            .compose(RxSchedulers.applySchedulers())
            .subscribe(NetDisposableObserver3(object :
                NetDisposableObserver3.NetCallBack<AiChatResponse> {
                override fun onResponse(response: AiChatResponse) {
                    if(TranslationInterior.isDebug()){
                        Log.d("AiTranslateManager", "makeChatTask response: $response")
                    }
                    if(response.code == 10000) {
                        callback?.invoke(response)
                    }else{
                        callback?.invoke(AiChatResponse(response.code, "", response.message, ""))
                    }
                }

                override fun onError(code: Int, msg: String) {
                    callback?.invoke(AiChatResponse(code, "", msg, ""))
                }

                override fun onTokenInvalid(msg: String) {
                    callback?.invoke(AiChatResponse(101231, "", "AI任务已过期", ""))
                }
            }))
    }

}